<template>
  <div v-if="isOpen" class="modal-overlay" @click.self="onClose">
    <div class="modal-container" :class="{ 'modal-enter': isOpen, 'modal-leave': !isOpen }">
      <div class="modal-header">
        <h2 class="modal-title">阴阳师登录</h2>
        <button class="close-btn" @click="onClose" aria-label="关闭">
          <svg width="24" height="24" viewBox="0 0 24 24" fill="none">
            <path d="M18 6L6 18" stroke="var(--gold-primary)" stroke-width="2" stroke-linecap="round" />
            <path d="M6 6L18 18" stroke="var(--gold-primary)" stroke-width="2" stroke-linecap="round" />
          </svg>
        </button>
      </div>
      <div class="modal-body">
        <form @submit.prevent="handleSubmit" class="login-form">
          <!-- 账号输入 -->
          <div class="form-group">
            <label for="username" class="form-label">账号</label>
            <input
              id="username"
              v-model="form.username"
              type="text"
              placeholder="请输入账号"
              required
              class="form-input"
            />
          </div>
          
          <!-- 密码输入 -->
          <div class="form-group">
            <label for="password" class="form-label">密码</label>
            <input
              id="password"
              v-model="form.password"
              type="password"
              placeholder="请输入密码"
              required
              class="form-input"
            />
          </div>
          
          <!-- 记住密码和忘记密码 -->
          <div class="form-options">
            <label class="remember-label">
              <input type="checkbox" v-model="form.remember" class="checkbox" />
              <span>记住密码</span>
            </label>
            <a href="#" class="forgot-password">忘记密码？</a>
          </div>
          
          <!-- 错误信息 -->
          <div v-if="errorMessage" class="error-message">{{ errorMessage }}</div>
          
          <!-- 登录按钮 -->
          <button type="submit" class="login-btn">登录</button>
          
          <!-- 第三方登录 -->
          <div class="third-party-login">
            <div class="divider">
              <span>其他方式登录</span>
            </div>
            <div class="third-party-buttons">
              <button type="button" class="third-party-btn wechat">
                <svg width="24" height="24" viewBox="0 0 24 24" fill="none">
                  <path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.41 0-8-3.59-8-8s3.59-8 8-8 8 3.59 8 8-3.59 8-8 8z" fill="#07C160"/>
                  <path d="M10 9.5c-.83 0-1.5.67-1.5 1.5s.67 1.5 1.5 1.5 1.5-.67 1.5-1.5-.67-1.5-1.5-1.5zm4 0c-.83 0-1.5.67-1.5 1.5s.67 1.5 1.5 1.5 1.5-.67 1.5-1.5-.67-1.5-1.5-1.5zm2 3c-1.66 0-3-1.34-3-3h-2c0 2.76 2.24 5 5 5s5-2.24 5-5H16z" fill="#07C160"/>
                </svg>
                <span>微信登录</span>
              </button>
              <button type="button" class="third-party-btn qq">
                <svg width="24" height="24" viewBox="0 0 24 24" fill="none">
                  <path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.41 0-8-3.59-8-8s3.59-8 8-8 8 3.59 8 8-3.59 8-8 8z" fill="#12B7F5"/>
                  <path d="M8.5 13.5c-.83 0-1.5-.67-1.5-1.5s.67-1.5 1.5-1.5 1.5.67 1.5 1.5-.67 1.5-1.5 1.5zm7 0c-.83 0-1.5-.67-1.5-1.5s.67-1.5 1.5-1.5 1.5.67 1.5 1.5-.67 1.5-1.5 1.5z" fill="#12B7F5"/>
                  <path d="M13 10.5c0-1.66-1.34-3-3-3s-3 1.34-3 3v1c0 .55.45 1 1 1s1-.45 1-1v-1c0-.55.45-1 1-1s1 .45 1 1c0 1.66 1.34 3 3 3s3-1.34 3-3c0-.55-.45-1-1-1s-1 .45-1 1z" fill="#12B7F5"/>
                </svg>
                <span>QQ登录</span>
              </button>
            </div>
          </div>
          
          <!-- 注册链接 -->
          <div class="register-link">
            还没有账号？
            <button type="button" class="register-button" @click="goToRegister">立即注册</button>
          </div>
        </form>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'

// Props
const props = defineProps({
  isOpen: {
    type: Boolean,
    default: false
  }
})

// Emits
const emit = defineEmits(['close', 'goToRegister', 'loginSuccess'])

// 表单数据
const form = ref({
  username: '',
  password: '',
  remember: false
})

const errorMessage = ref('')

// 处理关闭事件
const onClose = () => {
  emit('close')
  resetForm()
}

// 处理表单提交
const handleSubmit = () => {
  // 这里添加登录验证逻辑
  if (!form.value.username || !form.value.password) {
    errorMessage.value = '请填写完整的账号和密码'
    return
  }
  
  // 模拟登录请求
  console.log('登录信息:', form.value)
  
  // 模拟登录成功
  emit('close')
  resetForm()
  
  // 触发登录成功的事件
  const userData = {
    username: form.value.username,
    id: Date.now() // 模拟用户ID
  }
  emit('loginSuccess', userData)
}

// 跳转到注册页面
const goToRegister = () => {
  emit('close')
  emit('goToRegister')
}

// 重置表单
const resetForm = () => {
  form.value = {
    username: '',
    password: '',
    remember: false
  }
  errorMessage.value = ''
}
</script>

<style scoped>
/* 全局变量 - 从应用中继承或定义 */
:root {
  --gold-primary: #d4af37;
  --gold-secondary: #f0e68c;
  --gold-tertiary: #ffd700;
  --border-color: rgba(212, 175, 55, 0.3);
  --bg-primary: #0d0d0d;
  --bg-secondary: #1a1a1a;
  --bg-modal: rgba(0, 0, 0, 0.95);
  --text-primary: #ffffff;
  --text-secondary: #cccccc;
}

/* 遮罩层 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.8);
  backdrop-filter: blur(5px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
  animation: fadeIn 0.3s ease;
}

/* 弹窗容器 */
.modal-container {
  background: var(--bg-modal);
  border: 1px solid var(--border-color);
  border-radius: 12px;
  width: 100%;
  max-width: 500px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 10px 40px rgba(212, 175, 55, 0.3), 0 0 60px rgba(212, 175, 55, 0.1) inset;
  animation: slideIn 0.4s ease;
  position: relative;
}

/* 弹窗头部 */
.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 24px;
  border-bottom: 1px solid var(--border-color);
}

.modal-title {
  font-size: 24px;
  font-weight: bold;
  color: var(--gold-primary);
  text-shadow: 0 0 10px rgba(212, 175, 55, 0.5);
}

.close-btn {
  background: none;
  border: none;
  cursor: pointer;
  padding: 5px;
  border-radius: 50%;
  transition: all 0.3s ease;
}

.close-btn:hover {
  background: rgba(212, 175, 55, 0.1);
  transform: rotate(90deg);
}

/* 弹窗内容 */
.modal-body {
  padding: 24px;
}

/* 表单样式 */
.login-form {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.form-label {
  font-size: 14px;
  color: var(--text-secondary);
}

.form-input {
  padding: 12px 16px;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  color: var(--text-primary);
  font-size: 16px;
  transition: all 0.3s ease;
}

.form-input:focus {
  outline: none;
  border-color: var(--gold-primary);
  box-shadow: 0 0 0 3px rgba(212, 175, 55, 0.2);
  background: rgba(255, 255, 255, 0.15);
}

.form-input::placeholder {
  color: rgba(255, 255, 255, 0.4);
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.remember-label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  color: var(--text-secondary);
  cursor: pointer;
}

.checkbox {
  accent-color: var(--gold-primary);
  cursor: pointer;
}

.forgot-password {
  font-size: 14px;
  color: var(--gold-primary);
  text-decoration: none;
  transition: color 0.3s ease;
}

.forgot-password:hover {
  color: var(--gold-secondary);
  text-shadow: 0 0 5px rgba(212, 175, 55, 0.5);
}

/* 错误信息 */
.error-message {
  background: rgba(255, 0, 0, 0.2);
  border: 1px solid rgba(255, 0, 0, 0.3);
  border-radius: 6px;
  color: #ff6b6b;
  padding: 10px;
  text-align: center;
  font-size: 14px;
}

/* 登录按钮 */
.login-btn {
  padding: 14px 20px;
  background: linear-gradient(135deg, var(--gold-primary), var(--gold-tertiary));
  border: none;
  border-radius: 6px;
  color: #000;
  font-size: 16px;
  font-weight: bold;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 4px 15px rgba(212, 175, 55, 0.4);
}

.login-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(212, 175, 55, 0.6);
  background: linear-gradient(135deg, var(--gold-secondary), var(--gold-tertiary));
}

.login-btn:active {
  transform: translateY(0);
}

/* 第三方登录 */
.third-party-login {
  margin-top: 10px;
}

.divider {
  display: flex;
  align-items: center;
  margin: 20px 0;
}

.divider::before,
.divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--border-color);
}

.divider span {
  padding: 0 16px;
  color: var(--text-secondary);
  font-size: 14px;
}

.third-party-buttons {
  display: flex;
  gap: 16px;
}

.third-party-btn {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 10px 16px;
  background: transparent;
  border: 1px solid var(--border-color);
  border-radius: 6px;
  color: var(--text-secondary);
  font-size: 14px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.third-party-btn:hover {
  background: rgba(212, 175, 55, 0.1);
  border-color: var(--gold-primary);
  transform: translateY(-2px);
}

/* 注册链接 */
.register-link {
  text-align: center;
  margin-top: 10px;
  color: var(--text-secondary);
  font-size: 14px;
}

.register-button {
  background: none;
  border: none;
  color: var(--gold-primary);
  cursor: pointer;
  font-size: 14px;
  transition: color 0.3s ease;
}

.register-button:hover {
  color: var(--gold-secondary);
  text-shadow: 0 0 5px rgba(212, 175, 55, 0.5);
}

/* 动画效果 */
@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateY(-20px) scale(0.95);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .modal-container {
    margin: 20px;
    width: calc(100% - 40px);
  }
  
  .modal-header,
  .modal-body {
    padding: 16px;
  }
  
  .modal-title {
    font-size: 20px;
  }
}
</style>